package cn.fxl.org.controller;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.basic.utils.ExcelUtils;
import cn.fxl.org.domain.Shop;
import cn.fxl.org.domain.ShopAuditLog;
import cn.fxl.org.domain.ShopDto;
import cn.fxl.org.query.ShopQuery;
import cn.fxl.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.Name;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API",description = "店铺的入驻")
public class ShopController {
    @Autowired
    private IShopService iShopService;

    /**
     * 获取所有
     * @return
     */
    @GetMapping
    @ApiOperation("获取所有")
    public List<Shop> getAll(){
       return iShopService.getAll();
    }

    /**
     * 获取单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("获取单个店铺")
    public Shop getOne(@PathVariable("id") Long id){
        return iShopService.getOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除单个店铺")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iShopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 新增和修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation("新增店铺")
    public AjaxResult saveAndUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                iShopService.save(shop);
            }else {
                iShopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 高级查询 和分页查询
     * @param shopQuery
     * @return
     */
    @PostMapping
    @ApiOperation("高级查询 和分页查询")
    public GmResult<Shop>querPage(@RequestBody ShopQuery shopQuery){
        return iShopService.Pagequery(shopQuery);
    }

    /**
     * 批量删除
     * @param a
     * @return
     */
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResult pathchdel(@RequestBody Long [] a){
        try {
            iShopService.pathchdel(a);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 店铺注册
     * 店铺管理员信息添加
     * 店铺信息添加
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation("店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            iShopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessExciption businessExciption){
            businessExciption.printStackTrace();
            return new AjaxResult(false,businessExciption.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation("审核通过")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            iShopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    /**
     * 审核驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation("驳回")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            iShopService.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 店铺管理员激活店铺
     * @param id
     */
    @ApiOperation("激活店铺")
    @GetMapping("/active/{id}")
    public void active(@PathVariable("id") Long id){
        iShopService.activeShop(id);
    }

    /**
     * 导出shop
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = iShopService.getAll();
// List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response
        ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    /**
     * 导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file")MultipartFile file){
//        MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    /**
     * Echarts图表
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return iShopService.getEcharts();
    }
}
